package com.greatchn.single_login.bean;

import cn.hutool.core.collection.CollUtil;
import com.greatchn.single_login.util.ApiAssert;
import com.greatchn.single_login.web.po.CustomClientDetails;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.ClientRegistrationException;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @CustomClientDetailsService: 自定义客户端详情
 * @author: ZBoHang
 * @time: 2023/2/17 10:36
 */
@Component
public class CustomClientDetailsService implements ClientDetailsService {

    public static final Map<String, ClientDetails> map;

    static {
        map = new ConcurrentHashMap<String, ClientDetails>() {
            {
                put("client01", new CustomClientDetails()
                        .setCusClientId("client01")
                        .setCusClientSecret("client_secret01")
                        .setCusAuthorizedGrantTypes(CollUtil.set(Boolean.FALSE, "authorization_code", "implicit", "client_credentials", "password", "refresh_token"))
                        .setCusScope(CollUtil.set(Boolean.FALSE, "all_resource"))
                        .setCusResourceIds(CollUtil.set(Boolean.FALSE, "resourceId01"))
                        .setCusAutoApprove(Boolean.TRUE)
                        .setCusRegisteredRedirectUri(CollUtil.set(Boolean.FALSE, "https://www.baidu.com/")));
                put("admin", new CustomClientDetails()
                        .setCusClientId("admin")
                        .setCusClientSecret("client_secret01")
                        .setCusAuthorizedGrantTypes(CollUtil.set(Boolean.FALSE, "authorization_code", "implicit", "client_credentials", "password", "refresh_token"))
                        .setCusScope(CollUtil.set(Boolean.FALSE, "all_resource"))
                        .setCusResourceIds(CollUtil.set(Boolean.FALSE, "resourceId01"))
                        .setCusAutoApprove(Boolean.TRUE)
                        .setCusRegisteredRedirectUri(CollUtil.set(Boolean.FALSE, "https://www.baidu.com/")));
            }
        };
    }


    @Override
    public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException {

        ApiAssert.noneBlank(clientId, "缺少客户端id!");

        return map.get(clientId);
    }
}
